package com.haople.test.trace.document;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * 
 * @author hp
 * 动态页面（js，ajax） 使用此抓取器
 *
 */
public class HttpUnitDocumentParserImpl implements DocumentParser {
	
	public String getContent(String url) {
		System.out.println("抓取url-->"+url);
		WebClient webClient=new WebClient(BrowserVersion.FIREFOX_38);
		try {
			webClient.setJavaScriptTimeout(30000);//设置JS执行的超时时间
			webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常
			webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常
			webClient.getOptions().setTimeout(30000);//设置“浏览器”的请求超时时间
			webClient.getOptions().setCssEnabled(false);//是否启用CSS
			webClient.getOptions().setJavaScriptEnabled(true); //很重要，启用JS
			webClient.waitForBackgroundJavaScript(50000);//设置JS后台等待执行时间
			webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX
			HtmlPage htmlPage=webClient.getPage(url);
			return htmlPage.asXml();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("抓取[ "+url+" ]异常");
		}finally{
			webClient.close();
		}
		return null;
	}

}
